package com.nsoft.utils;

import com.nsoft.objects.BookItem;
import com.nsoft.ui.R;
import com.nsoft.ui.ViewActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BookArrayAdapter extends ArrayAdapter<String>{
	private final Context context;
	private String[] values;
	private Bitmap[] bmps;
	
	public BookArrayAdapter(Context ctx,
				String[] vals,Bitmap[] _bmps){
		super(ctx,R.layout.booklist_row,vals);
		this.context=ctx;
		this.values=vals;
		this.bmps=_bmps;
	}
	
	@Override
	public View getView(int position, View convertView, 
									ViewGroup parent){
		LayoutInflater inflater=(LayoutInflater)context
				.getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		
		View rowView=inflater.inflate(
				R.layout.booklist_row,parent,false);
		TextView textView=(TextView)
					rowView.findViewById(R.id.label);
		ImageView imageView=(ImageView)
					rowView.findViewById(R.id.logo);
		ImageButton openBtn=(ImageButton)rowView.
				findViewById(R.id.bookOpenButton);
		openBtn.setOnClickListener(
				new OpenClickListener(rowView));
		ImageButton downloadBtn=(ImageButton)rowView.
				findViewById(R.id.bookDownloadButton);
		downloadBtn.setOnClickListener(
				new DownloadClickListener(rowView));
		
		String text=values[position];
		textView.setText(text);
		
		Bitmap bmp=this.bmps[position];
		imageView.setImageBitmap(bmp);
		
		return rowView;
	}
	
	private class OpenClickListener implements 
						View.OnClickListener{
		private View rowView;
		public OpenClickListener(View rv){
			this.rowView=rv;
		}
		
		@Override
		public void onClick(View v) {
			ListView bookLv=(ListView)rowView.getParent();
			int pos=bookLv.getPositionForView(rowView);
			
			Intent intent=new Intent(
					context,ViewActivity.class);
			intent.putExtra(
					Constant.booklist.BOOK_INDEX_TRANSFER,
					pos);
			context.startActivity(intent);
		}
		
	}
	
	private class DownloadClickListener implements
				View.OnClickListener{
		private View rowView;
		
		public DownloadClickListener(View rv){
			this.rowView=rv;
		}
		
		@Override
		public void onClick(View v) {
			ListView bookLv=(ListView)rowView.getParent();
			final int pos=bookLv.getPositionForView(rowView);
			
			AlertDialog.Builder alert=
					new AlertDialog.Builder(context);
			alert.setIcon(R.drawable.ic_help);
			alert.setTitle(Constant.DIALOG_CONFIRM).
				setMessage(Constant.booklist.BLF_CFM_DOWNLOAD).
				setPositiveButton(Constant.DIALOG_YES,
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(
								DialogInterface dialog, 
								int which) {
							BookItem book=engine.getBookItem(pos);
							new BookDownloader(context,rowView,
								book).execute();
						}
				}).
				setNegativeButton(Constant.DIALOG_NO,
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(
								DialogInterface dialog, 
								int which){
							dialog.cancel();
						}
					}).
				create().show();
		}
	}
	
	


}
